using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Services.Identity.Domain;
using Services.Identity.Domain.ValueObject;
using Services.Identity.WebApi.Request.Permission;
using Services.Initial.PublicProject.PublicClass.OutPutModels;
using Services.Initial.PublicProject.PublicMethods;

namespace Services.Identity.WebApi.Controllers;
[ApiController]
[Route("[controller]/[action]")]
public class PermissionController(IDomainService domainService) : ControllerBase
{
    private readonly IDomainService domainService = domainService;

    [HttpPost]
    public async Task<OutputModel<string>> AddPermission(AddPermissionRequest request)
    {
        var result = await this.domainService.AddRolePermissionsAsync(request.RoleName, [.. request.PermissionNameAndRouter]);
        if (result)
        {
            return OutPutMethod<string>.Success("添加成功");
        }
        else
        {
            return OutPutMethod<string>.Failure("添加失败");
        }
    }
    [HttpDelete]
    public async Task<OutputModel<string>> DeletePermission(DeletePermissionRequest request)
    {
        var result = await this.domainService.DeleteRolePermissionsAsync(request.RoleName, request.Permissions);
        if (result)
        {
            return OutPutMethod<string>.Success("删除成功");
        }
        else
        {
            return OutPutMethod<string>.Failure("删除失败");
        }
    }
    [HttpGet]
    [Authorize]
    public async Task<OutputModel<List<PermissionNameAndRouter>>> GetRolePermissions()
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var user = await this.domainService.GetUserInfoByIdAsync(id);
        var roleName = await this.domainService.GetRoleByUserIdAsync(user);
        var result = await this.domainService.GetRolePermissionsAsync(roleName);
        return OutPutMethod<List<PermissionNameAndRouter>>.Success(result, 0);
    }
}